export default function (req, res) {
    const { query } = req;
    const { orderId } = query;
  
    if (!orderId) {
      return res.status(400).json({
        code: 1,
        message: '缺少必需的参数：orderId'
      });
    }
  
    // Use a fixed format for dates to match the API spec
    const formatDate = (date) => {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    };
  
    // Generate mock data for the requested order ID
    const studentId = Math.floor(Math.random() * 100000) + 1;
    const orderType = Math.random() < 0.5 ? 1 : 2;
    const orderStatus = [1, 2, 3, 4, 5, 6, 7][Math.floor(Math.random() * 7)];
    const payType = [1, 2, 3, 4][Math.floor(Math.random() * 4)];
    const totalClasstime = orderType === 2 ? parseFloat((Math.random() * 100 + 10).toFixed(1)) : 0;
    const studentPaidAmount = parseFloat((Math.random() * 2000 + 100).toFixed(2));
    const usedClasstime = parseFloat((Math.random() * totalClasstime * 0.5).toFixed(1));
    const refundedClasstime = parseFloat(((totalClasstime - usedClasstime) * Math.random()).toFixed(1));
    const unusedClasstime = parseFloat((totalClasstime - usedClasstime - refundedClasstime).toFixed(1));
    const usedAmount = parseFloat((studentPaidAmount * (usedClasstime / totalClasstime || 0)).toFixed(2));
    const refundableAmount = parseFloat((studentPaidAmount - usedAmount).toFixed(2));
    const createTime = new Date(Date.now() - Math.floor(Math.random() * 90) * 24 * 60 * 60 * 1000);
    const updateTime = new Date(createTime.getTime() + Math.floor(Math.random() * 30) * 24 * 60 * 60 * 1000);
    
    const mockOrderDetail = {
      id: parseInt(orderId),
      student: {
        id: studentId,
        name: `学员${Math.floor(Math.random() * 100) + 1}`,
        sn: `SN${Math.floor(Math.random() * 9000) + 1000}`,
        grade: ["一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "初一", "初二", "初三", "初四", "高一", "高二", "高三", "大一", "大二", "大三", "大四", "大四以上"][Math.floor(Math.random() * 19)],
        school: `模拟学校${Math.floor(Math.random() * 5) + 1}`,
        trainAccount: Math.random() < 0.5,
      },
      orderNo: `ORD${Math.floor(Math.random() * 90000000) + 10000000}`,
    //   1:体验订单2:充值订单
      type: 1,
      status: orderStatus,
    //   支付方式   枚举值说明：1:交付课时2:外部支付3:智牛币支付4:在线支付:
      payType: 3,
      paySubType: [1, 2, 3, 4, 5][Math.floor(Math.random() * 5)],
      externalOrderNo: Math.random() < 0.3 ? `EXT${Math.floor(Math.random() * 90000000) + 10000000}` : null,
      payImage: Math.random() < 0.5 ? "https://example.com/pay-image.jpg" : null,
      remark: Math.random() < 0.5 ? `订单备注 ${Math.random().toString(36).substring(7)}` : null,
      signer: `签约人${Math.floor(Math.random() * 10) + 1}`,
      totalClasstime: totalClasstime,
      usedClasstime: usedClasstime,
      unusedClasstime: unusedClasstime,
      scheduledUntaughtClasstime: parseFloat((unusedClasstime * Math.random()).toFixed(1)),
      refundedClasstime: refundedClasstime,
      studentPaidAmount: studentPaidAmount,
      usedAmount: usedAmount,
      refundableAmount: refundableAmount,
      receivingCompanyName: "智牛爱英语",
      receivingCompanyAccount: "1234567890",
      unitPrice: 101,
      receivingTime: formatDate(createTime),
      payer: `付款人${Math.floor(Math.random() * 50) + 1}`,
      createTime: formatDate(createTime),
      updateTime: formatDate(updateTime),
      updater: {
        id: Math.floor(Math.random() * 100) + 1,
        name: `操作员${Math.floor(Math.random() * 20) + 1}`
      }
    };
  
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          message: '成功！',
          data: mockOrderDetail,
          timestamp: Date.now(),
          requestId: 'requestId_' + Math.random().toString(36).substring(2, 15),
          signature: 'signature_' + Math.random().toString(36).substring(2, 15),
        });
      }, 100);
    });
  }